;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Enterprise.Person", [])
        .controller("PersonCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            // tab标签展示布尔值
            $scope.activeFlag = [true, false, false];
            // 列表查询条件
            $scope.condition = {};
            // 表格列配置信息
            $scope.toolbarConfig = {};
            // 表格信息
            $scope.tableConfig = {
                fixedWidth: 80,
                scrollWidth: 1000,
                tableCol: [
                    {
                        name: "姓名",
                        dataField: "cname",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "岗位",
                        dataField: "latestJobName",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "联系方式",
                        dataField: "contactTel",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "身份证号",
                        dataField: "idNumber",
                        width: 180,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "性别",
                        dataField: "genderF",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "入职时间",
                        dataField: "hiredateF",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "是否兼职",
                        dataField: "partTimeJobF",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "第二岗位",
                        dataField: "secJob",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "地址",
                        dataField: "address",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 80,
                        isShow: true,
                        isFixed: true
                    }
                ]
            };
            $scope.markInfo = {
                curRow: -1
            };
            // 分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 人员列表信息
            $scope.dataList = [];
            // 人员详情信息
            $scope.detailInfo = { gender: 1, partTimeJob: 0 };

            // 切换配置表格列展示隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };
            // 设置表格宽度
            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: { left: $scope.tableConfig.fixedWidth + "px" }
                };
            };
            // 当前选中行
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            // 切换标签页
            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        $scope.detailInfo = { gender: 1, partTimeJob: 0 };
                        break;
                    default:
                        break;
                }
            };
            // 点击某一行
            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        break;
                    default:
                        break;
                }
            };

            // 查询人员列表
            $scope.search = function (curPage) {
                if (curPage) $scope.pageInfo.curPage = curPage;
                getPersonList();
            };
            function getPersonList() {
                var url = '/employee/list';
                var param = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                };
                ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    if (!res.value) return;
                    angular.forEach(res.value.list, function (item) {
                        item.genderF = $filter("TrueOrFalse")(item.gender, "sex");
                        item.hiredateF = $filter("YbDate")(item.hiredate, 'yyyy-MM-dd');
                        item.partTimeJobF = $filter("TrueOrFalse")(item.partTimeJob);
                    })
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                });
            }

            // 新增人员
            $scope.addPerson = function () {
                $scope.changeTab(1);
            };

            // 修改人员
            $scope.editPerson = function (item) {
                $scope.detailInfo = item;
                $scope.changeTab(2);
            };

            // 删除人员
            $scope.delPerson = function (item) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function (data) {
                    var url = '/employee/delete';
                    ybHttp.Post('device', url, [item.id]).then(function (res) {
                        if (res.code !== 0) {
                            ybWidget.ShowMessage(res, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        $scope.search(1);
                    });
                })
            };

            // 保存
            $scope.saveData = function (valid) {
                if (!valid) {
                    $scope.submitted = true;
                    return;
                }
                var url = '/employee/save';
                var param = angular.extend($scope.detailInfo);
                ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.search(1);
                    $scope.changeTab(0);
                });
            };

            // 取消
            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            function getJobs() {
                var url = '/employee/jobs';
                ybHttp.Get('device', url, {}).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.jobList = res.value || [];
                });
            }


            var btnPermissionCode = {
                personSave: window.ActionCode.personSave,
                personDel: window.ActionCode.personDel,
            };
            $scope.btnPermission = {
                personSave: true,
                personDel: true,
            };
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            // 初始化页面信息
            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                getJobs();
                $scope.setFixedWidth();
                $scope.search(1);
            })()
        }])
        ;
})(window, window.angular);